package com.faceboob.api;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import com.restfb.types.Page;

import facebook4j.Facebook;
import facebook4j.FacebookFactory;
import facebook4j.conf.ConfigurationBuilder;

public class FacebookUtil {
	// https://www.facebook.com/dialog/oauth?client_id=512719258786601&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=email,publish_stream&response_type=token&grant_type=email,publish_stream,create_event
	// https://graph.facebook.com/oauth/access_token?client_id=512719258786601&
	// redirect_uri=http://www.facebook.com/connect/login_success.html&
	// client_secret=34aab288a1ea54d1be9ac7836fe67720&code=MY_VERIFICATION_CODE
	public static void main2(final String[] args) {
		try {
			postOnPage(
					"BAAHSUMFKGykBABXpXxG3rZBV6K84I6iEYs4bEpq1sgmZBMIaFDAm8fNOFsPZCZAomHQAC70LohSYjInba8K9tZAmS51tzf2vq72B82ZBqPMpjz5K6L5XRAGYZBwULcL1UqEdiCdQQ2tWVFo4yJkUHK5Freh3h23SxHlZCokKXZAgJViMErjOO4wh7L68ZBv4Ms1pSOMFwnAqHNzmBRpPEaGTr4m0f0K1DDYnTp1ZBlmJr7khQZDZD",
					"http://offers.in-boutique.com/?itemId=B006K3ONSQ&price=$1.49", "Offersssssssssssssssss");
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public static void postOnPage(final String acess_token, final String link, final String message) {
		final FacebookClient facebookClient = new DefaultFacebookClient(acess_token);

		final FacebookType publishMessageResponse = facebookClient.publish("me/links", FacebookType.class, Parameter.with("link", link),
				Parameter.with("message", message));

		System.out.println("Published message ID: " + publishMessageResponse.getId());

	}

	public static void main4(final String[] args) {
		try {
			postOnPage(
					"CAAEJZAh36RdgBAME36GyhzwIxREGZBtSXeFwZCfyjirlcinSZC4gDjsloMcTRT6G9d0nu2kWZChpBuQSDZCa1wm4iJca2L5KZB5dfm61DxIHpL12LD62zNabXv8jPabVMy7Dn6ZAcUgiLzmjxkNBZA3MCY4wfv98zwzIZD",
					"http://www.5-mins.com/", "Yarab......");
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public static void main3(final String[] args) {
		try {
			final ConfigurationBuilder cb = new ConfigurationBuilder();
			cb.setDebugEnabled(true)
					.setOAuthAppId("291809170965976")
					.setOAuthAppSecret("942addd2fe45611442c9930382ae2527")
					.setOAuthAccessToken(
							"CAAEJZAh36RdgBAME36GyhzwIxREGZBtSXeFwZCfyjirlcinSZC4gDjsloMcTRT6G9d0nu2kWZChpBuQSDZCa1wm4iJca2L5KZB5dfm61DxIHpL12LD62zNabXv8jPabVMy7Dn6ZAcUgiLzmjxkNBZA3MCY4wfv98zwzIZD")
					.setOAuthPermissions("email,publish_stream,create_event");
			final FacebookFactory ff = new FacebookFactory(cb.build());
			// -----------------------------
			final Facebook facebook = ff.getInstance();
			// facebook.setOAuthAppId(appId, appSecret);
			// facebook.setOAuthPermissions("email,publish_stream");
			// facebook.setOAuthAccessToken(new AccessToken(accessToken, null));

			// facebook.postStatusMessage("Hello World from Facebook4J.");

			// final PostUpdate post = new PostUpdate(new URL("http://www.5-mins.com/"))
			// .picture(
			// new URL(
			// "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRZMlh699woABfNu6JTvrfTKmrx_FCdqIQBYNEzcMOnrX-o1vhd"))
			// .name("لك الله يا مصر").caption("لك الله يا مصر").description("اللهم احفظ مصر و اهلها");
			// facebook.postFeed(post);

			System.out.println(facebook.getMe().getFirstName());
			System.out.println(facebook.getMe().getLastName());
			System.out.println(facebook.getMe().getUsername());
		} catch (final Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(final String[] args) {
		try {
			final FacebookClient facebookClient = new DefaultFacebookClient(
					"CAAEJZAh36RdgBALV329z2ykH8ybZBSZBxhuTRHkSkOAGrhZCKPnzqBb7YhfZAuwNwZC0xnwsMGOOKkfFG8rrDZCybrArRbmlvCYYSaZAZAS0ZCeAxaZCg7xiFeXVofw9M0v8vON126GiP63WNxDeyPm4JD2hzi7HXEcEoQZD");
			final Page page = facebookClient.fetchObject("508273689199533", Page.class);
			System.out.println(page.getCanPost());
			System.out.println(page.getAbout());
			System.out.println(page.getCategory());
			System.out.println(page.getDescription());
			System.out.println(page.getName());
			System.out.println(page.getIsPublished());
			System.out.println(page.getCover().getSource());

			System.out.println(page.getAccessToken());

			// final FacebookType publishMessageResponse = facebookClient.publish("feed/links", FacebookType.class,
			// Parameter.with("link", "http://www.5-mins.com/"), Parameter.with("message", "yarab"));

			final FacebookType publishMessageResponses = facebookClient.publish("508273689199533/feed", FacebookType.class,
					Parameter.with("message", "yarab starak wa redak"));

			System.out.println("Published message ID: " + publishMessageResponses.getId());
			// System.out.println("Published message ID: " + publishMessageResponse.getId());
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
}
